import request from '@/utils/request'
import { UserQuery, UserResponse, UserInfo, ProfileUpdateData } from '@/types/user'
// 登录接口
export function login(data: {
  username: string
  password: string
  remember: boolean
}) {
  return request({
    url: '/auth/login',
    method: 'post',
    data
  })
}

// 注册接口
export function register(data: any) {
  return request({
    url: '/auth/register',
    method: 'post',
    data
  })
}

// 获取用户信息
export function getUserInfo() {
  return request({
    url: '/auth/info',
    method: 'get'
  })
}

// 退出登录
export function logout() {
  return request({
    url: '/auth/logout',
    method: 'post'
  })
}


// 获取用户列表
export const getUserList = (params: UserQuery) => {
  return request<UserResponse>({
    url: '/user/list',
    method: 'get',
    params
  })
}

// 添加用户
export function addUser(data: UserInfo) {
  return request({
    url: '/user',
    method: 'post',
    data
  })
}

// 更新用户
export function updateUser(id: number, data: UserInfo) {
  return request({
    url: `/user/${id}`,
    method: 'put',
    data
  })
}

// 删除用户
export const deleteUser = (id: number) => {
  return request({
    url: `/user/${id}`,
    method: 'DELETE'
  })
}


// 添加更新个人信息的API
export function updateUserProfile(data: ProfileUpdateData) {
  return request({
    url: '/user/profile',
    method: 'put',
    data
  })
}

// 修改密码
export function updatePassword(data: { oldPassword: string; newPassword: string }) {
  return request({
    url: '/user/password',
    method: 'put',
    data
  })
}

/**
 * 重置用户密码
 * @param userId 用户ID
 * @param password 新密码
 * @returns Promise
 */
export function resetUserPassword(userId: number, password: string) {
  return request({
    url: `/user/${userId}/reset-password`,
    method: 'post',
    data: { password }
  })
}

// 头像上传
export function uploadAvatar(file: File) {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: '/avatar',
    method: 'post',
    data: formData,
  })
}

// 获取用户邮箱
export function getEmailByUsername(username: string) {
  return request({
    url: '/auth/email',
    method: 'get',
    params: { username }
  })
}